package projet.dao.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import projet.dao.ClientDAOInterface;
import projet.entity.Client;
import projet.entity.Session;

public class JPAClientDAO implements ClientDAOInterface {

	@Override
	public List<Client> getList() {
		List<Client> clients = null;

		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();

			TypedQuery<Client> query = em.createNamedQuery("Client.findAll", Client.class);
			clients = query.getResultList();

			em.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			try { em.getTransaction().rollback(); } catch (Exception e2) { e2.printStackTrace(); }
		} finally {
			try { em.close(); } catch (Exception e2) { e2.printStackTrace(); }
		}

		return clients;
	}

	@Override
	public Client getById(Long id) {
		Client client = null;

		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();

			client = em.find(Client.class, id);

			em.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			try { em.getTransaction().rollback(); } catch (Exception e2) { e2.printStackTrace(); }
		} finally {
			try { em.close(); } catch (Exception e2) { e2.printStackTrace(); }
		}

		return client;
	}

	@Override
	public void create(Client client) {
		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();

			em.persist(client);

			em.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			try { em.getTransaction().rollback(); } catch (Exception e2) { e2.printStackTrace(); }
		} finally {
			try { em.close(); } catch (Exception e2) { e2.printStackTrace(); }
		}
	}

	@Override
	public void update(Client client) {
		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();

			em.merge(client);

			em.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			try { em.getTransaction().rollback(); } catch (Exception e2) { e2.printStackTrace(); }
		} finally {
			try { em.close(); } catch (Exception e2) { e2.printStackTrace(); }
		}
	}

	@Override
	public void delete(Client client) {
		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();

			Client c = em.find(Client.class, client.getId());
			em.remove(c);

			em.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			try { em.getTransaction().rollback(); } catch (Exception e2) { e2.printStackTrace(); }
		} finally {
			try { em.close(); } catch (Exception e2) { e2.printStackTrace(); }
		}
	}

	@Override
	public Session getSession(Long id) {
		Session session = null;

		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();

			TypedQuery<Session> query = em.createNamedQuery("Client.getSession", Session.class);
			query.setParameter("id", id);
			session = query.getSingleResult();

			em.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			try { em.getTransaction().rollback(); } catch (Exception e2) { e2.printStackTrace(); }
		} finally {
			try { em.close(); } catch (Exception e2) { e2.printStackTrace(); }
		}

		return session;
	}

}
